package com.store.app.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.adapter.TaskDetialsListAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.NewTaskDetialsBean;
import com.store.app.bean.TaskDetailsListBean;
import com.store.app.bean.TaskHeadBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/3/7.
 * 新任务详情页面
 */
public class NewTaskDetailsActivity extends BaseActivity implements IOnHttpListener {
    private LinearLayout submit;                    //参与
    private ImageView public_ll_return;
    private TextView tvTitle;
    private ListView lv;
    private TaskDetialsListAdapter adapter;
    private List<TaskDetailsListBean> taskDetailsListBeanList;
    private String task_id;
    private NetRequest netRequest;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_task_details);
        ExitApplication.getInstance().addActivity(this);
        initView();
        if (getIntent() != null) {
            task_id = getIntent().getStringExtra("task_id");
            netRequest = new NetRequest(this);
            netRequest.requestNewTaskDetails(1, task_id);
        }
    }

    private void initView() {
        submit = (LinearLayout) findViewById(R.id.submit);
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("详情");
        lv = (ListView) findViewById(R.id.lv);
        taskDetailsListBeanList = new ArrayList<>();
        adapter = new TaskDetialsListAdapter(this, taskDetailsListBeanList);
        lv.setAdapter(adapter);

        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(NewTaskDetailsActivity.this);
                String login_phone = preferences.getString("login_phone", "");
                if (!TextUtils.isEmpty(login_phone)) {
                    netRequest.requestJoinTask(2, task_id, login_phone);
                }
            }
        });
        public_ll_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                jsonObject.getString("detail");
                Gson gson = new Gson();
                NewTaskDetialsBean newTaskDetialsBean = gson.fromJson(jsonObject.getString("detail").toString(), NewTaskDetialsBean.class);
                TaskHeadBean taskHeadBean = new TaskHeadBean();
                taskHeadBean.setTv_expiration_date(newTaskDetialsBean.getExpried_date());
                taskHeadBean.setTv_reward(newTaskDetialsBean.getAmount());
                taskHeadBean.setPayment_way_key(newTaskDetialsBean.getPayment_way_key());
                taskHeadBean.setTv_content(newTaskDetialsBean.getDesc1());
                taskHeadBean.setTv_content_title(newTaskDetialsBean.getTask_name());
                adapter.setTaskHeadBean(taskHeadBean);
                Map<String, String> map = Utils.getMapForJson(jsonObject.getString("doc_url"));
                JSONArray jsonArray = new JSONArray(newTaskDetialsBean.getJson_data());
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject temJsonObj = jsonArray.getJSONObject(i);
                    String path_id = temJsonObj.getString("path_id");
                    String title = temJsonObj.getString("title");
                    String url = map.get(path_id);
                    TaskDetailsListBean taskDetailsListBean = new TaskDetailsListBean();
                    taskDetailsListBean.setPicUrl(url);
                    taskDetailsListBean.setTitle(title);
                    taskDetailsListBeanList.add(taskDetailsListBean);
                }
                adapter.notifyDataSetChanged();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //成功之后跳转到待提审的详情页面
        else if (requestId == 2) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String processing_id = jsonObject.getString("processing_id");
                Intent intent = new Intent(this, WillSubmitTaskDetailsActivity.class);
                intent.putExtra("processing_id", processing_id);
                startActivity(intent);
                EventBus.getDefault().post(new EventBusBean(224));
                finish();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        showToast(msg);
    }
}
